﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Managers {
    import flash.events.*;
    import Engine.Events.*;
    import Engine.Transactions.*;
    import Engine.Managers.*;
    import Events.*;
    import Classes.*;
    import __AS3__.vec.*;
    import Init.*;

    public class QuestManager extends EventDispatcher {

        private static var m_instance:QuestManager;

        private var m_initialized:Boolean = false;
        private var m_activeQuests:Vector.<Object>;

        public function QuestManager(){
            this.m_activeQuests = new Vector.<Object>();
            super();
        }
        public static function getInstance():QuestManager{
            if (m_instance == null){
                m_instance = new (QuestManager)();
            };
            return (m_instance);
        }

        public function initialize():void{
            if (this.m_initialized == false){
                this.m_initialized = true;
                TransactionManager.getInstance().addEventListener(TransactionEvent.ADDED, this.onTransactionAdded);
                TransactionManager.getInstance().addEventListener(TransactionEvent.COMPLETED, this.onTransactionComplete);
            };
        }
        public function getActiveQuests():Vector.<Quest>{
            var _local2:Object;
            var _local3:Quest;
            var _local1:Vector.<Quest> = new Vector.<Quest>();
            for each (_local2 in this.m_activeQuests) {
                _local3 = QuestSettingsInit.getItemByName(_local2.name);
                if (_local3 != null){
                    _local1.push(_local3);
                };
            };
            return (_local1);
        }
        public function getIncompleteActiveQuests():Vector.<Quest>{
            var _local2:Object;
            var _local3:Quest;
            var _local1:Vector.<Quest> = new Vector.<Quest>();
            for each (_local2 in this.m_activeQuests) {
                if (!_local2.complete){
                    _local3 = QuestSettingsInit.getItemByName(_local2.name);
                    if (_local3 != null){
                        _local1.push(_local3);
                    };
                };
            };
            return (_local1);
        }
        public function getQuestProgressByName(_arg1:String):Object{
            var _local2:int = this.getQuestProgressIndexByName(_arg1);
            return (((_local2)>=0) ? this.m_activeQuests[_local2] : null);
        }
        public function setQuestTaskProgress(_arg1:String, _arg2:int, _arg3:int):void{
            if (!QuestSettingsInit.isClientTestingEnabled){
                return;
            };
            var _local4:int = this.getQuestProgressIndexByName(_arg1);
            var _local5:Object = this.m_activeQuests[_local4];
            _local5.progress[_arg2] = _arg3;
            var _local6:Quest = QuestSettingsInit.getItemByName(_arg1);
            this.performClientProgressUpdate(_local6, _local5, {
                progressChange:true,
                taskCompleted:_arg2
            });
        }
        protected function onTransactionAdded(_arg1:TransactionEvent):void{
            var _local3:Object;
            var _local4:Quest;
            var _local5:Object;
            if (!QuestSettingsInit.isClientTestingEnabled){
                return;
            };
            GlobalEngine.log("QuestManager", ("Transaction added: " + _arg1.transaction));
            var _local2:int;
            while (_local2 < this.m_activeQuests.length) {
                _local3 = this.m_activeQuests[_local2];
                _local4 = QuestSettingsInit.getItemByName(_local3.name);
                if ((((_local4 == null)) || (_local3.complete))){
                } else {
                    _local5 = this.updateProgress(_local4, _local3, _arg1.transaction);
                    this.performClientProgressUpdate(_local4, _local3, _local5);
                };
                _local2++;
            };
        }
        protected function performClientProgressUpdate(_arg1:Quest, _arg2:Object, _arg3:Object):void{
            var _local4:QuestEvent;
            if (this.checkQuestCompletion(_arg2, _arg1)){
                _arg2.complete = true;
                dispatchEvent(new QuestEvent(QuestEvent.COMPLETED, _arg1));
            } else {
                if (_arg3.progressChange){
                    _local4 = new QuestEvent(QuestEvent.PROGRESS, _arg1);
                    _local4.taskCompleted = _arg3.taskCompleted;
                    dispatchEvent(_local4);
                };
            };
        }
        protected function onTransactionComplete(_arg1:TransactionEvent):void{
            var _local4:int;
            var _local5:Object;
            var _local6:Quest;
            var _local7:Array;
            var _local8:int;
            var _local9:Boolean;
            var _local10:int;
            var _local11:int;
            var _local12:QuestEvent;
            var _local2 = !(QuestSettingsInit.isClientTestingEnabled);
            var _local3:Object;
            if (_arg1.transaction.rawResult){
                if (_arg1.transaction.rawResult.metadata == null){
                    return;
                };
                _local3 = _arg1.transaction.rawResult.metadata.QuestComponent;
            };
            GlobalEngine.log("QuestManager", ("Transaction complete: " + _arg1.transaction));
            if (_local3 != null){
                _local4 = 0;
                while (_local4 < _local3.length) {
                    _local5 = _local3[_local4];
                    _local6 = QuestSettingsInit.getItemByName(_local5.name);
                    if (_local6 == null){
                    } else {
                        if (_local5["extraData"]){
                            _local6.addExtraData(_local5.extraData);
                        };
                        _local7 = _local3[_local4].purchased;
                        if (_local7){
                            _local8 = 0;
                            while ((((_local8 < _local7.length)) && ((_local8 < _local6.purchased.length)))) {
                                _local9 = _local7[_local8];
                                _local6.purchased[_local8] = ((_local9) || (_local6.purchased[_local8]));
                                _local8++;
                            };
                        };
                        if (_local5.expired == true){
                            dispatchEvent(new QuestEvent(QuestEvent.EXPIRED, _local6));
                        } else {
                            if ((((_local5.complete == true)) && (_local2))){
                                dispatchEvent(new QuestEvent(QuestEvent.COMPLETED, _local6));
                            } else {
                                _local10 = -1;
                                _local11 = this.getQuestProgressIndexByName(_local5.name);
                                if (_local11 < 0){
                                    this.m_activeQuests.push(_local5);
                                };
                                if (_local2){
                                    if (_local11 >= 0){
                                        _local10 = this.checkTaskCompletion(this.m_activeQuests[_local11].progress, _local5.progress, _local6);
                                        this.m_activeQuests[_local11] = _local5;
                                    };
                                    _local12 = new QuestEvent(QuestEvent.PROGRESS, _local6);
                                    _local12.taskCompleted = _local10;
                                    dispatchEvent(_local12);
                                };
                            };
                        };
                    };
                    _local4++;
                };
            };
        }
        protected function getQuestProgressIndexByName(_arg1:String):int{
            var _local2:int;
            while (_local2 < this.m_activeQuests.length) {
                if (this.m_activeQuests[_local2].name == _arg1){
                    return (_local2);
                };
                _local2++;
            };
            return (-1);
        }
        protected function checkTaskCompletion(_arg1:Array, _arg2:Array, _arg3:Quest):int{
            var _local5:int;
            var _local4:int;
            while (_local4 < _arg3.tasks.length) {
                _local5 = int(_arg3.tasks[_local4].total);
                if (((!((_arg1[_local4] == _local5))) && ((_arg2[_local4] >= _local5)))){
                    return (_local4);
                };
                _local4++;
            };
            return (-1);
        }
        protected function checkQuestCompletion(_arg1:Object, _arg2:Quest):Boolean{
            var _local4:Object;
            var _local3:int;
            while (_local3 < _arg2.tasks.length) {
                _local4 = _arg2.tasks[_local3];
                if (_arg1.progress[_local3] < _local4.total){
                    return (false);
                };
                _local3++;
            };
            return (true);
        }
        protected function updateProgress(_arg1:Quest, _arg2:Object, _arg3:Transaction):Object{
            var task:* = null;
            var value:* = NaN;
            var fn:* = null;
            var quest:* = _arg1;
            var status:* = _arg2;
            var transaction:* = _arg3;
            if ((((((((transaction == null)) || ((status == null)))) || ((status.expired == true)))) || ((status.complete == true)))){
                return ({});
            };
            var newProgress:* = new Array(quest.tasks.length);
            var progressChange:* = false;
            var taskCompleted:* = -1;
            var i:* = 0;
            while (i < quest.tasks.length) {
                task = quest.tasks[i];
                if (task.action == null){
                } else {
                    GlobalEngine.info("QuestManager", ((("checking quest " + quest.name) + ", task action ") + task.action));
                    value = 0;
                    if (quest.purchased[i]){
                        value = task.total;
                    } else {
                        try {
                            fn = QuestSettingsInit.getClientTestingClass[task.action];
                            value = fn.apply(null, [transaction, task.type, quest.extraData]);
                        } catch(err:Error) {
                            trace(err);
                        };
                    };
                    switch (task.stickyType){
                        case StickyType.MAX:
                            newProgress[i] = this.getMaxProgress(value, status.progress[i]);
                            break;
                        case StickyType.ACCUM:
                            newProgress[i] = this.getAccumProgress(value, status.progress[i]);
                            break;
                        case StickyType.NONE:
                        default:
                            if (task.sticky == true){
                                newProgress[i] = this.getAccumProgress(value, status.progress[i]);
                            } else {
                                newProgress[i] = this.getNormalProgress(value);
                            };
                    };
                    if (newProgress[i] != status.progress[i]){
                        progressChange = true;
                    };
                    if ((((status.progress[i] < task.total)) && ((newProgress[i] >= task.total)))){
                        taskCompleted = i;
                    };
                };
                i = (i + 1);
            };
            status.progress = newProgress;
            return ({
                progressChange:progressChange,
                taskCompleted:taskCompleted
            });
        }
        private function getNormalProgress(_arg1:int):int{
            return (_arg1);
        }
        private function getMaxProgress(_arg1:int, _arg2:int):int{
            return (Math.max(_arg1, _arg2));
        }
        private function getAccumProgress(_arg1:int, _arg2:int):int{
            return ((_arg2 + _arg1));
        }

    }
}//package Managers 
